package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.pojo.Specification;
import com.leyou.item.service.SpecificationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @author: Mr_Sun
 * @Date: 2018/9/6
 * @Time: 11:09
 */
@RestController
@RequestMapping("/spec")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;
    /**
     * 规格参数添加
     * tb_spec_group
     * tb_spec_param  group_id
     * 添加的是category分类   我写错了  以后写  就重写
     *
     */
//    @PostMapping("group")
//    public ResponseEntity<Void> addSpecGroup(SpecParam specParam){
//         specificationService.addSpecGroup(specParam);
//
//        return ResponseEntity.status(HttpStatus.OK).build();
//    }

    /**
     * 规格参数查询
     * - 请求方式：GET
     * - 请求路径：/spec/params
     * - 请求参数：gid，分组id
     * - 返回结果：该分组下的规格参数集合List<SpecParam>
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParam>> querySpecParam(
            @RequestParam(value = "gid", required = false) Long gid,
            @RequestParam(value = "cid", required = false) Long cid,
            @RequestParam(value = "searching", required = false) Boolean searching,
            @RequestParam(value = "generic", required = false) Boolean generic
    ) {
        List<SpecParam> list = specificationService.querySpecParam(gid, cid, searching, generic);
        if (list == null || list.size() == 0) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);

        }
        return ResponseEntity.status(HttpStatus.OK).body(list);
    }

    @GetMapping("/groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecificationByCategoryId(@PathVariable("cid") Long cid) {
        List<SpecGroup> list = specificationService.querySpecGroups(cid);
        if (list == null || list.size() == 0) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);

        }
        return ResponseEntity.status(HttpStatus.OK).body(list);
    }

    /**
     * url: '/item/spec',
     * data: this.$qs.stringify({
     * categoryId: this.currentNode.id,
     * specifications: JSON.stringify(this.specifications)
     * })
     * 保存规格模板功能
     */
    @PostMapping
    public ResponseEntity<Void> saveTemplate(Specification specification) {

        if (StringUtils.isBlank(specification.getSpecifications())) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();

        }
        specificationService.saveTemplate(specification);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @PutMapping
    public ResponseEntity<Void> updateTemplate(Specification specification) {

        if (StringUtils.isBlank(specification.getSpecifications())) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();

        }
        specificationService.updateTemplate(specification);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @GetMapping("group/param/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupWithParam(@PathVariable("cid") Long cid) {
        List<SpecGroup> groups = this.specificationService.queryGroupWithParam(cid);
        if (CollectionUtils.isEmpty(groups)) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();

        }
        return ResponseEntity.ok(groups);
    }
}
